<?php

class ShippingsController extends AppController {

    var $name = 'Shippings';
    var $uses = array('Category', 'Shipping');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow = array('*');
    }

    function index($id = null) {
        $this->layout = false;
        $this->Category->recursive = -1;
        $categories = $this->Category->findById($id);
        $this->set(compact('categories'));
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid shipping', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('shipping', $this->Shipping->read(null, $id));

        $this->set(compact('categories'));
    }

    function add($id=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('The shipping Cost has been saved', true));
            } else {
                $this->Session->setFlash(__('The shipping Cost could not be saved', true));
            }
        }
        $this->render('success');
    }

    function addshipping() {
        $this->layout = false;
        $this->Shipping->recursive = -1;
        $shippings = $this->Shipping->find("all");
        $this->set('shippings', $shippings);
    }

    function shippingmethodadd($id=null) {
        $this->layout = false;
        if (!empty($this->data)) {
            $this->Shipping->create();
            if ($this->Shipping->save($this->data)) {
                $this->Session->setFlash(__('The shipping Method has been saved', true));
                $this->redirect(array('action' => 'addshipping'));
            } else {
                $this->Session->setFlash(__('The shipping Method could not be saved. Please, try again.', true));
            }
        }
    }

    function edit($id = null) {
        $this->layout = false;
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid shipping', true));
            $this->redirect(array('action' => 'addshipping'));
        }
        if (!empty($this->data)) {
            if ($this->Shipping->save($this->data)) {
                $this->Session->setFlash(__('The shipping has been updated.', true));
                $this->redirect(array('action' => 'addshipping'));
            } else {
                $this->Session->setFlash(__('The shipping could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->Shipping->recursive = -1;
            $this->data = $this->Shipping->read(null, $id);
        }
    }

    function delete($id = null) {

        if (!$id) {
            $this->Session->setFlash(__('Invalid id for shipping', true));
            $this->redirect(array('action' => 'addshipping'));
        }
        $query = "DELETE FROM authentic_shippings WHERE id='" . $id . "' LIMIT 1";
        if ($this->Shipping->query($query)) {
            $this->Session->setFlash(__('Shipping method has been deleted', true));
            $this->redirect(array('action' => 'addshipping'));
        }
        $this->Session->setFlash(__('Shipping was not deleted', true));
        $this->redirect(array('action' => 'addshipping'));
    }

    function backstage_index() {
        $this->Shipping->recursive = 0;
        $this->set('shippings', $this->paginate());
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid shipping', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('shipping', $this->Shipping->read(null, $id));
    }

    function backstage_add($id=null) {
        if (!empty($this->data)) {
            $this->Shipping->create();
            if ($this->Shipping->save($this->data)) {
                $this->Session->setFlash(__('The shipping has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The shipping could not be saved. Please, try again.', true));
            }
        }
        $categories = $this->Shipping->Category->find('list', array('fields' => 'Category.category_name'));
        $this->set(compact('categories'));
    }

    function backstage_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid shipping', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Shipping->save($this->data)) {
                $this->Session->setFlash(__('The shipping has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The shipping could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Shipping->read(null, $id);
        }
        $categories = $this->Shipping->Category->find('list');
        $this->set(compact('categories'));
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for shipping', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Shipping->delete($id)) {
            $this->Session->setFlash(__('Shipping deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Shipping was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}

?>